package com.example.readwrite;

import java.util.Arrays;

public class SharedData {
    private volatile char[] data;
    private ReadWriteLock readWriteLock = new ReadWriteLock();

    public SharedData() {
        this(8);
    }

    public SharedData(int size) {
        this.data = new char[size];
        Arrays.fill(this.data, '*');
    }

    public char[] read() {
        readWriteLock.readLock();
        char[] chars = Arrays.copyOf(data, data.length);
        readWriteLock.readUnlock();
        return chars;
    }

    public void write(char ch) {
        readWriteLock.writeLock();
        char[] chars = Arrays.copyOf(data, data.length);
        Arrays.fill(chars, ch);
        data = chars;
//        Arrays.fill(data, ch);
        readWriteLock.writeUnlock();
    }
}
